package com.hongtu.xaseataorg1.service;


import com.hongtu.xaseataorg1.dao.AccountInfoMapper;
import com.hongtu.xaseataorg1.fegin.Bank2Client;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountInfoServiceImpl implements AccountInfoService {
    private Logger logger = LoggerFactory.getLogger(AccountInfoServiceImpl.class);

    @Autowired
    AccountInfoMapper accountInfoDao;
    @Autowired
    Bank2Client bank2Client;
    @GlobalTransactional
    @Transactional
    @Override
    public void updateAccountBalance(String accountNo, Double amount) {
        accountInfoDao.updateAccountBalance(accountNo, amount * -1);
        String remoteRst = bank2Client.transfer(amount);
//制造异常
        if(amount==2){
            throw new RuntimeException("转账测试");
        }
        //远程调用失败
        if(remoteRst.equals("error")){
            throw new RuntimeException("降级处理");
        }

    }
}
